import Vue from 'vue'
import Vuex from 'vuex'

import {
  sysWebColumnQuery,
  sysWebColumnDelete,
  sysWebColumnAdd,
  sysWebColumnUpdate,
  sysWebColumnOpenApiQuery,
  sysWebColumnDeleteBatch,
} from '@/api/module/sysWebColumn'

Vue.use(Vuex)

const state = {
  curColumn: {},
  columnData: [],
  homeColumnData: [],
}

var delNoChild = function (list) {
  list.forEach((v) => {
    v['disable'] = v.isOutsite == 1
    v.childList.length == 0 ? delete v.childList : delNoChild(v.childList)
  })
  return list
}

const getters = {
  curColumn: (state) => state.curColumn || {},
  columnData(state) {
    let { columnData } = state
    return delNoChild(columnData)
  },
  homeColumnData: (state) => state.homeColumnData || [],
}

const actions = {
  async queryWebColumn({ commit }, listQuery) {
    let res = await sysWebColumnQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteWebColumn({ commit }, row) {
    let res = await sysWebColumnDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteBatchWebColumn({ commit }, ids) {
    let res = await sysWebColumnDeleteBatch(ids)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addWebColumn({ commit }, data) {
    let res = await sysWebColumnAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateWebColumn({ commit }, data) {
    let res = await sysWebColumnUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async queryOpenApiWebColumn({ commit }, listQuery) {
    let res = await sysWebColumnOpenApiQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  SET_COLUMN: (state, d) => {
    state.curColumn = d
  },
  SET_COLUMNDATA: (state, d) => {
    state.columnData = d
  },
  SET_HOMECOLUMNDATA: (state, d) => {
    state.homeColumnData = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
